package com.feicheng.service.impl;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import javax.annotation.Resource;

import com.feicheng.persistence.entity.*;
import com.feicheng.persistence.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feicheng.common.constant.Constant;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.common.wx.pay.WxPayBean;
import com.feicheng.persistence.model.CardTypeMain;
import com.feicheng.persistence.model.ClientMain;
import com.feicheng.persistence.model.CutDepositRecordMain;
import com.feicheng.persistence.model.DepositBackRequestMain;
import com.feicheng.persistence.model.DepositRecordMain;
import com.feicheng.persistence.model.VipMain;
import com.feicheng.persistence.model.VoucherRecordMain;
import com.feicheng.service.BaseService;
import com.feicheng.service.IFClientService;


@Service
public class FClientServiceImpl extends BaseService implements IFClientService {

    @Autowired
    IVoucherRecordService voucherRecordService;

    @Autowired
    IVoucherTypeService voucherTypeService;

    @Autowired
    IClientService clientService;

    @Autowired
    IAccountService accountService;

    @Autowired
    IOrderService orderService;

    @Autowired
    IStoreClientService storeClientService;

    @Autowired
    IVipService vipService;

    @Autowired
    IClientVipService clientVipService;

    @Autowired
    ICardTypeService cardTypeService;

    @Autowired
    IPriceService priceService;

    @Autowired
    IDepositRecordService depositRecordService;

    @Autowired
    IClientDepositService clientDepositService;

    @Autowired
    IPaymentRecordService paymentRecordService;

    @Autowired
    IDepositBackRequestService depositBackRequestService;

    @Autowired
    IStoreService storeService;

    @Autowired
    ICutDepositRecordService cutDepositRecordService;

    @Autowired
    IOrderConsumeService orderConsumeService;

    @Autowired
    IConsumeRecordService consumeRecordService;

    @Autowired
    IOrderService iOrderService;

    @Autowired
    IAccountService iAccountService;

    @Autowired
    IStoreClientService iStoreClientService;

    @Autowired
    ICardRecordService cardRecordService;

    @Autowired
    IVoucherTypeService iVoucherTypeService;

    @Autowired
    IDepositService iDepositService;

    @Resource(name="store-wx-pay")
    Map<String, WxPayBean> storeWxPayService;

    @Transactional
    @Override
    public ResultInfo cutDeposit(Integer amount,
                                 String reason,
                                 Integer clientId) {
        
        User currentUser = super.currentUser();
        LocalDateTime currentDateTime = super.ct(true);

        //判断账户余额够不够
        QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<Account>();
        accountQueryWrapper.eq(Constant.ENTITY.ACCOUNT.CLIENT_ID, clientId);

        List<Account> accounts = accountService.list(accountQueryWrapper);

        if (1 != accounts.size()) {
            for (Account account : accounts) {
                Client exceptionClient = new Client();
                exceptionClient.setClientId(account.getClientId());
                exceptionClient.setDFlg(true);
                clientService.updateById(exceptionClient);
            }
            throw new SystemException("当前用户非法，已经冻结，请联系管理员。");
        }

        Account account = accounts.remove(0);

        if (account.getDepositAmount() < amount){
            throw new SystemException("余额不足");//余额不足
        }

        //账户表修改
        account.setDepositAmount(account.getDepositAmount() - amount);
        accountService.updateById(account);

        //新增金额，原因，clientId，扣押状态
        CutDepositRecord cutDepositRecord = new CutDepositRecord();
        cutDepositRecord.setClientId(clientId);
        cutDepositRecord.setAmount(amount);
        cutDepositRecord.setReason(reason);
        cutDepositRecord.setCt(currentDateTime);
        cutDepositRecord.setUu(currentUser.getUserId());
        cutDepositRecord.setUt(currentDateTime);
        cutDepositRecordService.save(cutDepositRecord);

        return ResultInfo.success();
    }

    @Transactional
    @Override
    public ResultInfo cancelCutDeposit(Integer cutId) {

        User currentUser = super.currentUser();
        LocalDateTime currentDateTime = super.ct(true);

        CutDepositRecord cutDepositRecord = cutDepositRecordService.getById(cutId);
        cutDepositRecord.setIoFlg(true);
        cutDepositRecord.setUu(currentUser.getUserId());
        cutDepositRecord.setUt(currentDateTime);
        cutDepositRecordService.updateById(cutDepositRecord);

        //判断账户余额够不够
        QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<Account>();
        accountQueryWrapper.eq(Constant.ENTITY.ACCOUNT.CLIENT_ID, cutDepositRecord.getClientId());

        List<Account> accounts = accountService.list(accountQueryWrapper);

        if (1 != accounts.size()) {
            for (Account account : accounts) {
                Client exceptionClient = new Client();
                exceptionClient.setClientId(account.getClientId());
                exceptionClient.setDFlg(true);
                clientService.updateById(exceptionClient);
            }
            throw new SystemException("当前用户非法，已经冻结，请联系管理员。");
        }

        Account account = accounts.remove(0);

        //账户表修改
        account.setDepositAmount(account.getDepositAmount() + cutDepositRecord.getAmount());
        accountService.updateById(account);

        return ResultInfo.success();
    }

    @Override
    public synchronized ResultInfo rejectDeposit(Integer backId, String reason) {
        throw new SystemException("方法弃用");
    }

    @Override
    public ResultInfo backDeposit(Integer backId) {
    	throw new SystemException("方法弃用");
    }

    @Override
    public JSONArray getVipIDsByClientId(Integer clientId) {
        JSONArray result = new JSONArray();
        QueryWrapper<ClientVip> clientVipQueryWrapper = new QueryWrapper<>();
        clientVipQueryWrapper.eq(Constant.ENTITY.CLIENT_VIP.CLIENT_ID, clientId);

        List<ClientVip> cLientVips = clientVipService.list(clientVipQueryWrapper);

        if (null != cLientVips && cLientVips.size() > 0) {
            for (ClientVip clientVip : cLientVips) {
                result.add(clientVip.getVipId());
            }
        }
        return result;
    }

    /**
     * 分页查询客户信息
     * @see com.feicheng.service.IFClientService#getClientsByPage(JSONObject)
     *徐航
     */
    @Override
    public ResultInfo getClientsByPage(JSONObject param) {

        QueryWrapper<ClientMain> queryWrapper = new QueryWrapper<ClientMain>();
        if (null != param.getBoolean(Constant.ENTITY.CLIENT.D_FLG)){
            queryWrapper.eq(Constant.ENTITY.CLIENT.D_FLG,param.get(Constant.ENTITY.CLIENT.D_FLG));
        }
        //根据客户姓名查询
        if(StringUtils.hasLength(param.getString(Constant.ENTITY.CLIENT.CLIENT_NAME))) {
            queryWrapper.like(Constant.ENTITY.CLIENT.CLIENT_NAME, param.get(Constant.ENTITY.CLIENT.CLIENT_NAME));
        }
        //根据店名姓名查询
        if(StringUtils.hasLength((String) param.get(Constant.ENTITY.STORE.NAME))) {
            queryWrapper.like(Constant.ENTITY.STORE.NAME, param.get(Constant.ENTITY.STORE.NAME));
        }
        //根据门店ID查询
//        if(null != param.getInteger(Constant.ENTITY.STORE.STORE_ID)){
//            queryWrapper.eq(Constant.ENTITY.STORE.STORE_ID, param.getInteger(Constant.ENTITY.STORE.STORE_ID));
//        }
        //根据手机号查询
        if (StringUtils.hasLength(param.getString(Constant.ENTITY.CLIENT.PHONE))) {
            queryWrapper.like(Constant.ENTITY.CLIENT.PHONE, param.getString(Constant.ENTITY.CLIENT.PHONE));
        }
        //根据性别查询
        if(null != param.getBoolean(Constant.ENTITY.CLIENT.SEX)) {
            queryWrapper.eq(Constant.ENTITY.CLIENT.SEX, param.getBoolean(Constant.ENTITY.CLIENT.SEX));
        }

        if(null != param.getBoolean(Constant.ENTITY.VIP.STATU)){
            if(param.getBoolean(Constant.ENTITY.VIP.STATU)){
                queryWrapper.eq("vip_statu", 1);
            } else {
                queryWrapper.isNull("vip_statu");
            }
        }

        //当前页
        Integer yema = param.getIntValue(Constant.HTTP.PARAM.CURRENT);
        //总页
        Integer zongye = param.getInteger(Constant.HTTP.PARAM.SIZE);

        queryWrapper.orderByDesc(Constant.ENTITY.CLIENT.CLIENT_ID);

        //分页查询,调用service接口方法（传参），接返回值
        Page<ClientMain> result = clientService.getClientsByPage(new Page<ClientMain>(yema, zongye), queryWrapper);
        return ResultInfo.success(result);
    }

    /**
     * 删除客户信息
     * @see com.feicheng.service.IFClientService#deleteClient(Integer clientId)
     *徐航
     */
    @Override
    public ResultInfo deleteClient(Integer clientId) {
        Client delClient = new Client();
        delClient.setClientId(clientId);
        delClient.setDFlg(true);
        //更改dflag
        clientService.updateById(delClient);
        return ResultInfo.success();
    }

    /**
     * 更新客户信息
     * @see com.feicheng.service.IFClientService#updateClient(Client)
     * @Author 徐航
     * @param client 客户信息更新
     */
    @Override
    public ResultInfo updateClient(Client client) {
        clientService.updateById(client);
        return ResultInfo.success(clientService.getById(client.getClientId()));
    }

    /**
     * @see com.feicheng.service.IFClientService#getClient(Integer)
     * @param clientId 主键：客户编号
     * @Author 徐航
     * @return
     */
    @Override
    public ResultInfo getClient(Integer clientId) {

        JSONObject result = new JSONObject();

        /* 根据主键取得客户基本信息 */
        Client client = clientService.getById(clientId);
        result.put(Constant.ENTITY.CLIENT.TABLE, client);

        /* 根基客户主键 取得客户的账户信息 */
        QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
        accountQueryWrapper.eq(Constant.ENTITY.ACCOUNT.CLIENT_ID, clientId);

        List<Account> accounts = accountService.list(accountQueryWrapper);
        if (1 != accounts.size()) {
            client.setDFlg(true);
            clientService.updateById(client);
            // TODO "用户"+client.getClientId()+":"+client.getClientName()+"存在异常，已经冻结，请联系管理员"
            throw new SystemException("用户"+client.getClientId()+":"+client.getClientName()+"存在异常，已经冻结，请联系管理员");
        }
        result.put(Constant.ENTITY.ACCOUNT.TABLE, accounts.remove(0));

        /* 根基客户主键 取得客户的门店客户相关信息 */
        QueryWrapper<StoreClient> storeClientQueryWrapper = new QueryWrapper<>();
        storeClientQueryWrapper.eq(Constant.ENTITY.STORE_CLIENT.CLIENT_ID, clientId);
        storeClientQueryWrapper.eq(Constant.ENTITY.STORE_CLIENT.WX_TYPE, Constant.VAL.WX_TYPE.S);

//        List<StoreClient> storeClients = storeClientService.list(storeClientQueryWrapper);
//        if (1 != storeClients.size()) {
//            client.setDFlg(true);
//            clientService.updateById(client);
//            // TODO "用户"+client.getClientId()+":"+client.getClientName()+"存在异常，已经冻结，请联系管理员"
//            throw new SystemException("用户"+client.getClientId()+":"+client.getClientName()+"存在异常，已经冻结，请联系管理员");
//        }
//
//        StoreClient storeClient = storeClients.remove(0);
//        result.put(Constant.ENTITY.STORE_CLIENT.TABLE, storeClient);

        /* 根基门店主键 取得门店信息 */
//        Store store = storeService.getById(storeClient.getStoreId());
//        result.put(Constant.ENTITY.STORE.TABLE, store);

        /* 取得客户的押金信息 */
        JSONArray depositIds = new JSONArray();

        QueryWrapper<ClientDeposit> clientDepositQueryWrapper = new QueryWrapper<>();
        clientDepositQueryWrapper.eq(Constant.ENTITY.CLIENT_DEPOSIT.CLIENT_ID, clientId);
        List<ClientDeposit> clientDeposits = clientDepositService.list(clientDepositQueryWrapper);
        if (clientDeposits.size() > 0) {
            for (ClientDeposit clientDeposit : clientDeposits) {
                depositIds.add(clientDeposit.getDepositId());
            }
        }

        QueryWrapper<DepositBackRequest> depositBackRequestQueryWrapper = new QueryWrapper<>();
        depositBackRequestQueryWrapper.eq(Constant.ENTITY.DEPOSIT_BACK_REQUEST.CLIENT_ID, clientId);
//        depositBackRequestQueryWrapper.isNotNull(Constant.ENTITY.DEPOSIT_BACK_REQUEST.DEPOSIT_ID);
        List<DepositBackRequest> depositBackRequests = depositBackRequestService.list(depositBackRequestQueryWrapper);
        result.put(Constant.ENTITY.DEPOSIT_BACK_REQUEST.TABLE, depositBackRequests);

        /* 取得客户会员信息 */
        QueryWrapper<ClientVip> clientVipQueryWrapper = new QueryWrapper<>();
        clientVipQueryWrapper.eq(Constant.ENTITY.CLIENT_VIP.CLIENT_ID, clientId);
        List<ClientVip> clientVips = clientVipService.list(clientVipQueryWrapper);
        JSONArray vipIds = new JSONArray();
        for (ClientVip clientVip : clientVips) {
            vipIds.add(clientVip.getVipId());
        }

        if (vipIds.size() > 0) {
            QueryWrapper<Vip> vipQueryWrapper = new QueryWrapper<>();
            vipQueryWrapper.in(Constant.ENTITY.VIP.VIP_ID, vipIds);
            List<Vip> vips = vipService.list(vipQueryWrapper);
            result.put(Constant.ENTITY.VIP.TABLE, vips);
        }

        /* 取得客户订单信息 */
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq(Constant.ENTITY.ORDER.CLIENT_ID, clientId);

        List<Order> orders = orderService.list(orderQueryWrapper);
        result.put(Constant.ENTITY.ORDER.TABLE, orders);

        return ResultInfo.success(result);
    };

    /**
     * 会员分页查询
     * @see com.feicheng.service.IFClientService#getVipsByPage(VipMain vipMain, Integer current, Integer size)
     * 李志
     */

    @Override
    public ResultInfo getVipsByPage(VipMain vipMain, Integer current, Integer size) {

        //创建qw对象
        QueryWrapper<VipMain> queryWrapper = new QueryWrapper<VipMain>();

        //根据客户ID查询
        if (null != vipMain.getClientId()) {
            queryWrapper.eq(Constant.ENTITY.CLIENT.CLIENT_ID, vipMain.getClientId());
        }

        //根据客户姓名查询
        if (StringUtils.hasLength(vipMain.getClientName())) {
            queryWrapper.like(Constant.ENTITY.CLIENT.CLIENT_NAME, vipMain.getClientName());
        }
        //根据会员类别查询（年卡，月卡等）
        if (StringUtils.hasLength(vipMain.getVipType())) {
            queryWrapper.eq(Constant.ENTITY.VIP.VIP_TYPE, vipMain.getVipType());
        }
        //根据门店名称查询
        if (StringUtils.hasLength(vipMain.getName())){
            queryWrapper.eq(Constant.ENTITY.STORE.NAME, vipMain.getName());
        }
        //根据会员开始结束时间区间进行查询
        if (null != vipMain.getEt()){
            queryWrapper.le(Constant.ENTITY.VIP.ET, vipMain.getEt());
        }
        if (null != vipMain.getSt()){
            queryWrapper.ge(Constant.ENTITY.VIP.ST, vipMain.getSt());
        }

//        if (null != vipMain.getVoucherRecordId()){
//            queryWrapper.isNotNull(Constant.ENTITY.VIP.VOUCHER_RECORD_ID);
//        }
//        if (null != vipMain.getCardRecordId()){
//            queryWrapper.isNotNull(Constant.ENTITY.VIP.CARD_RECORD_ID);
//        }

        if (null != vipMain.getStatu()){
            queryWrapper.eq(Constant.ENTITY.VIP.STATU, vipMain.getStatu());
        }
        
        if (StringUtils.hasLength(vipMain.getPhone())){
            queryWrapper.eq("phone", vipMain.getPhone());
        }
        //根据卡券类型id查询
        if (null != vipMain.getCardTypeId()){
            queryWrapper.eq(Constant.ENTITY.CARD_TYPE.CARD_TYPE_ID, vipMain.getCardTypeId());
        }
        if (null != vipMain.getVoucherTypeId()){
            queryWrapper.eq(Constant.ENTITY.VOUCHER_TYPE.VOUCHER_TYPE_ID, vipMain.getVoucherTypeId());
        }
        //根据门店id查询
        if(null != vipMain.getStoreId()){
            queryWrapper.eq(Constant.ENTITY.STORE.STORE_ID, vipMain.getStoreId());
        }

        //根据支付平台支付编号查询
        if(null != vipMain.getOriginalCode() && vipMain.getOriginalCode().length() > 0){
            queryWrapper.like(Constant.ENTITY.PAYMENT_RECORD.ORIGINAL_CODE, vipMain.getOriginalCode());
        }

        queryWrapper.orderByDesc(Constant.ENTITY.VIP.VIP_ID);

        //返回
        return ResultInfo.success(vipService.getVipsByPage(new Page<VipMain>(current, size), queryWrapper));
    }
    /**
     * 会员详细页面
     * @see com.feicheng.service.IFClientService# getVip(Integer vipId)
     * 李志
     */
    @Override
    public ResultInfo getVip(Integer vipId) {
        //创建qw对象
        QueryWrapper<VipMain> queryWrapper =  new QueryWrapper<VipMain>();
        //传输vipId
        queryWrapper.eq(Constant.ENTITY.VIP.VIP_ID,vipId);
        //调用service
        VipMain result = vipService.getVipById(queryWrapper);
        return ResultInfo.success(result);
    }
    /**
     * 会员删除页面
     * @see com.feicheng.service.IFClientService# deleteVip(Integer vipId)
     * 李志
     */
    @Override
    public ResultInfo deleteVip(Integer vipId) {
        QueryWrapper<Vip> queryWrapper = new QueryWrapper<Vip>();
        queryWrapper.eq(Constant.ENTITY.VIP.VIP_ID,vipId);
        vipService.remove(queryWrapper);
        return ResultInfo.success();
    }
    /**
     * 会员更新页面
     * @see com.feicheng.service.IFClientService# deleteVip(Integer vipId)
     * 李志
     */
    @Override
    public ResultInfo updateVip(JSONObject param) {

        //修改vip表对象
        vipService.updateById(param.getObject(Constant.ENTITY.VIP.TABLE,Vip.class));
        return ResultInfo.success();
    }
    /**
     * 会员新增页面
     * @see com.feicheng.service.IFClientService# newVip(JSONObject param)
     * 李志
     */
    @Override
    public ResultInfo newVip(JSONObject param){
        //创建会员对象以及会员客户中间表对象
        Vip vip = new Vip();
        //查询是否有支付记录
        if (StringUtils.hasLength((String) param.get(Constant.ENTITY.PAYMENT_RECORD.PAY_TYPE))){
            //有支付记录则是会员卡新增
            //录入支付记录
            PaymentRecord paymentRecord = new PaymentRecord();
            paymentRecord.setCt(LocalDateTime.now());
            paymentRecord.setPayType(param.getString(Constant.ENTITY.PAYMENT_RECORD.PAY_TYPE));
            paymentRecord.setIoFlg(param.getBoolean(Constant.ENTITY.PAYMENT_RECORD.IO_FLG));
            paymentRecord.setOriginalCode(param.getString(Constant.ENTITY.PAYMENT_RECORD.ORIGINAL_CODE));
            paymentRecordService.save(paymentRecord);
           //将种类名称放入vip对象
            vip.setVipType(Constant.ENTITY.VIP.C);
            //将会员相关信息放入vip对象
            vip.setCt(LocalDateTime.now());
            //录入会员卡记录
            CardRecord cardRecord = new CardRecord();
            cardRecord.setCardTypeId(param.getInteger(Constant.ENTITY.CARD_RECORD.CARD_TYPE_ID));
            cardRecord.setAmount(param.getInteger(Constant.ENTITY.CARD_RECORD.AMOUNT));
            QueryWrapper<CardType> cardTypeQueryWrapperNew =new QueryWrapper<CardType>();
            cardTypeQueryWrapperNew.eq(Constant.ENTITY.CARD_TYPE.CARD_TYPE_ID,param.get(Constant.ENTITY.CARD_TYPE.CARD_TYPE_ID));
            cardRecord.setTimeLongs(cardTypeService.getOne(cardTypeQueryWrapperNew).getTimeLongs());
            //放入到cardRecord对象中
            cardRecord.setPayId(paymentRecord.getPayId());
            cardRecordService.save(cardRecord);
            cardRecord.getRecordId();
            vip.setCardRecordId(cardRecord.getRecordId());
            vipService.save(vip);
            vip.getVipId();
            //录入vip客户中间表
            //录入中间表
            ClientVip clientVipNew = new ClientVip();
            clientVipNew.setVipId(vip.getVipId());
            clientVipNew.setClientId(param.getInteger(Constant.ENTITY.CLIENT.CLIENT_ID));
            clientVipService.save(clientVipNew);


        }
        else{
            //无支付记录则是会员卷新增
            //添加会员卷信息
            VoucherRecord voucherRecord = new VoucherRecord();
             //录入vip数据
            Vip vipNew = new Vip();
            vipNew.setCt(LocalDateTime.now());
            vipNew.setCardRecordId(null);
            vipNew.setVoucherRecordId(param.getInteger(Constant.ENTITY.VOUCHER_RECORD.RECORD_ID));
            vipNew.setVipType(Constant.ENTITY.VIP.V);
            vipService.save(vipNew);
            //录入中间表
            ClientVip clientVipNew = new ClientVip();
            clientVipNew.setVipId(vipNew.getVipId());
            clientVipService.save(clientVipNew);
        }
            return ResultInfo.success();
    }
    /**
     * 分页查询会员卡类型
     * @see com.feicheng.service.IFClientService# getCardTypesByPage(CardType, Integer, Integer)
     * 杜飞
     */
    @Override
    public ResultInfo getCardTypesByPage(CardType cardType, Integer current, Integer size) {
        QueryWrapper<CardTypeMain> queryWrapper = new QueryWrapper<CardTypeMain>();
        queryWrapper.eq(Constant.ENTITY.VOUCHER_TYPE.D_FLG,false);
        //根据会员卡类型名称查
        if (StringUtils.hasLength(cardType.getName())){
            queryWrapper.like(Constant.ENTITY.CARD_TYPE.NAME,cardType.getName());
        }

        //根据会员卡状态查询
        if (null != cardType.getStatu()){
            queryWrapper.eq(Constant.ENTITY.CARD_TYPE.STATU,cardType.getStatu());
        }

        //分页查询
        Page<CardTypeMain> result = cardTypeService.getCardTypesByPage(new Page<CardTypeMain>(current,size),queryWrapper);
        return ResultInfo.success(result);
    }

    /**
     * 修改会员卡基本信息
     * @see com.feicheng.service.IFClientService# updateCardType(CardType cardType, Integer price)
     * 杜飞
     */
    @Override
    public ResultInfo updateCardType(CardType cardType, Integer price) {
        cardType.setTimeLongs(cardType.getTimeLongs()*60*24);
        QueryWrapper<CardType> cardTypeQueryWrapper = new QueryWrapper<>();
        cardTypeQueryWrapper.eq(Constant.ENTITY.CARD_TYPE.CARD_TYPE_ID,cardType.getCardTypeId());
        cardTypeService.update(cardType,cardTypeQueryWrapper);
        //修改会员卡价格
        QueryWrapper<Price> priceQueryWrapper = new QueryWrapper<>();
        priceQueryWrapper.eq(Constant.ENTITY.PRICE.CARD_TYPE_ID,cardType.getCardTypeId());
        Price updatePrice = new Price();
        updatePrice.setAmount(price);
        priceService.update(updatePrice,priceQueryWrapper);
        return ResultInfo.success();
    }

    /**
     * 新增会员卡类型
     * @see com.feicheng.service.IFClientService# newCardType(CardType cardType, Integer price)
     * 杜飞
     */
    @Override
    @Transactional
    public ResultInfo newCardType(CardType cardType, Integer price) {
        cardType.setTimeLongs(cardType.getTimeLongs()*60*24);
        cardTypeService.save(cardType);
        //新增会员卡价格
        Price newPrice = new Price();
        newPrice.setCardTypeId(cardType.getCardTypeId());
        newPrice.setAmount(price);
        priceService.save(newPrice);
        return ResultInfo.success();
    }

    /**
     * 会员卡种类删除
     * @see com.feicheng.service.IFClientService# deleteCardType(Integer cardTypeId)
     * 杜飞
     */
    @Override
    public ResultInfo deleteCardType(Integer cardTypeId) {
        //会员卡类型id不为空
        if(null == cardTypeId){
            throw new SystemException("");
        }
        //构建会员卡种类实体类对象
        CardType cardType = new CardType();
        QueryWrapper<CardType> cardTypeQueryWrapper = new QueryWrapper<>();
        cardTypeQueryWrapper.eq(Constant.ENTITY.CARD_TYPE.CARD_TYPE_ID,cardTypeId);
        cardType.setDFlg(true);
        //更新会员卡种类表
        cardTypeService.update(cardType,cardTypeQueryWrapper);
        return ResultInfo.success();
    }

    /**
     * 取得会员卡类型详情
     * @see com.feicheng.service.IFClientService#getCardTypes(CardType cardType)
     * 杜飞
     */
    @Override
    public ResultInfo getCardTypes(CardType cardType) {
        QueryWrapper<CardTypeMain> cardTypeQueryWrapper = new QueryWrapper<>();
        if (null != cardType.getDFlg()) {
            cardTypeQueryWrapper.eq(Constant.ENTITY.CARD_TYPE.D_FLG, cardType.getDFlg());
        }

        if (null != cardType.getStatu()) {
            cardTypeQueryWrapper.eq(Constant.ENTITY.CARD_TYPE.STATU, cardType.getStatu());
        }

        List<CardTypeMain> cardTypeList = cardTypeService.getCardTypes(cardTypeQueryWrapper);
        return ResultInfo.success(cardTypeList);
    }

    /**
     * 通过id取得会员卡详情
     * @see com.feicheng.service.IFClientService#getCardTypeById(Integer cardTypeId)
     * 杜飞
     */
    @Override
    public ResultInfo getCardTypeById(Integer cardTypeId) {
        if (null == cardTypeId){
            throw new SystemException("");
        }
        QueryWrapper<CardType> cardTypeQueryWrapper = new QueryWrapper<CardType>();
        cardTypeQueryWrapper.eq(Constant.ENTITY.CARD_TYPE.CARD_TYPE_ID,cardTypeId);
        List<CardType> cardTypeList = cardTypeService.list(cardTypeQueryWrapper);

        QueryWrapper<Price> priceQueryWrapper = new QueryWrapper<Price>();
        priceQueryWrapper.eq(Constant.ENTITY.PRICE.CARD_TYPE_ID,cardTypeList.get(0).getCardTypeId());
        List<Price> priceList = priceService.list(priceQueryWrapper);

        CardTypeMain cardTypeMain = new CardTypeMain();
        cardTypeMain.setCardTypeId(cardTypeId);
        cardTypeMain.setName(cardTypeList.get(0).getName());
        cardTypeMain.setTimeLongs(cardTypeList.get(0).getTimeLongs()/60/24);
        cardTypeMain.setStatu(cardTypeList.get(0).getStatu());
        cardTypeMain.setAmount(priceList.get(0).getAmount());
        return ResultInfo.success(cardTypeMain);

    }

    /**
     * 分页查询会员券类型信息
     * @see com.feicheng.service.IFClientService# getVoucherTypesByPage(JSONObject param)
     * 杜飞
     */
    @Override
    public ResultInfo getVoucherTypesByPage(JSONObject param) {
        //取得当前页和总页数
        Integer current = (Integer) param.get(Constant.HTTP.PARAM.CURRENT);
        Integer size = (Integer) param.get(Constant.HTTP.PARAM.SIZE);
        QueryWrapper<VoucherType> queryWrapper = new QueryWrapper<VoucherType>();
        //判断d_Flg
        queryWrapper.eq(Constant.ENTITY.VOUCHER_TYPE.D_FLG,false);
        //根据会员券名称查询
        if (StringUtils.hasLength((String) param.get(Constant.ENTITY.VOUCHER_TYPE.NAME))){
            queryWrapper.like(Constant.ENTITY.VOUCHER_TYPE.NAME,param.get(Constant.ENTITY.VOUCHER_TYPE.NAME));
        }
        //根据会员券状态查询
        if (null != param.get(Constant.ENTITY.VOUCHER_TYPE.STATU) && !"".equals(param.get(Constant.ENTITY.VOUCHER_TYPE.STATU))){
            queryWrapper.eq(Constant.ENTITY.VOUCHER_TYPE.STATU,param.get(Constant.ENTITY.VOUCHER_TYPE.STATU));
        }
        //分页查询
        QueryWrapper<VoucherType> voucherTypeQueryWrapper = new QueryWrapper<VoucherType>();
        Page<VoucherType> result = voucherTypeService.getVoucherTypesByPage(new  Page<VoucherType>(current,size),queryWrapper);
        return ResultInfo.success(result);

    }

    /**
     * 修改会员券类型
     * @see com.feicheng.service.IFClientService# updateVoucherType(VoucherType voucherType)
     * 杜飞
     */
    @Override
    public ResultInfo updateVoucherType(VoucherType voucherType) {
        voucherType.setTimeLongs(voucherType.getTimeLongs()*60*24);
        VoucherType updateVoucherType = new VoucherType();
        updateVoucherType.setVoucherTypeId(voucherType.getVoucherTypeId());
        updateVoucherType.setName(voucherType.getName());
        updateVoucherType.setTimeLongs(voucherType.getTimeLongs());
        updateVoucherType.setStatu(voucherType.getStatu());
        voucherTypeService.updateById(updateVoucherType);
        return ResultInfo.success();
    }

    /**
     * 新增会员券类型
     * @see com.feicheng.service.IFClientService# newVoucherType(VoucherType voucherType)
     * 杜飞
     */
    @Override
    public ResultInfo newVoucherType(VoucherType voucherType) {
        VoucherType newVoucherType = new VoucherType();
        newVoucherType.setName(voucherType.getName());
        newVoucherType.setTimeLongs(voucherType.getTimeLongs()*60*24);
        newVoucherType.setStatu(voucherType.getStatu());
        voucherTypeService.save(newVoucherType);
        return ResultInfo.success();
    }

    /**
     * 会员券种类删除
     * @see com.feicheng.service.IFClientService# deleteVoucherType(Integer voucherTypeId)
     * 杜飞
     */
    @Override
    public ResultInfo deleteVoucherType(Integer voucherTypeId) {
        //构建会员券种类实体类对象
        VoucherType voucherType = new VoucherType();
        voucherType.setVoucherTypeId(voucherTypeId);
        voucherType.setDFlg(true);
        //更新会员券种类列表
        voucherTypeService.updateById(voucherType);
        return ResultInfo.success();
    }

    /**
     * 听过id查询会员券种类信息
     * @see com.feicheng.service.IFClientService# getVoucherTypeById(Integer voucherTypeId)
     * 杜飞
     */
    @Override
    public ResultInfo getVoucherTypeById(Integer voucherTypeId) {
        //判断主键是否为空
        if(null == voucherTypeId){
            throw new SystemException("");
        }
        QueryWrapper<VoucherType> voucherTypeQueryWrapper = new QueryWrapper<VoucherType>();
        voucherTypeQueryWrapper.eq(Constant.ENTITY.VOUCHER_TYPE.VOUCHER_TYPE_ID, voucherTypeId);
        List<VoucherType> voucherTypeList = voucherTypeService.list(voucherTypeQueryWrapper);
        VoucherType voucherType = new VoucherType();
        voucherType.setVoucherTypeId(voucherTypeList.get(0).getVoucherTypeId());
        voucherType.setName(voucherTypeList.get(0).getName());
        voucherType.setTimeLongs(voucherTypeList.get(0).getTimeLongs()/60/24);
        voucherType.setStatu(voucherTypeList.get(0).getStatu());
        return ResultInfo.success(voucherType);
    }

    /**
     * 取得所有会员券种类信息
     * @see com.feicheng.service.IFClientService#getVouchers(VoucherType voucherType)
     * 杜飞
     */
    @Override
    public ResultInfo getVouchers(VoucherType voucherType) {
        QueryWrapper<VoucherType> voucherTypeQueryWrapper = new QueryWrapper<>();
        if (null != voucherType.getVoucherTypeId()){
            voucherTypeQueryWrapper.eq(Constant.ENTITY.VOUCHER_TYPE.VOUCHER_TYPE_ID,voucherType.getVoucherTypeId());
        }
        //会员券种类状态为可用
        voucherTypeQueryWrapper.eq(Constant.ENTITY.VOUCHER_TYPE.STATU,true);
        //会员券种类未删除
        voucherTypeQueryWrapper.eq(Constant.ENTITY.VOUCHER_TYPE.D_FLG,false);
        return ResultInfo.success(voucherTypeService.list(voucherTypeQueryWrapper));
    }

    /**
     * 分页查询会员券详情
     * @see com.feicheng.service.IFClientService#getVouchersByPage(JSONObject param)
     * 姜浩男
     */
    @Override
    public ResultInfo getVouchersByPage(JSONObject param) {
        //取得当前页和总页数
        Integer current = (Integer) param.get(Constant.HTTP.PARAM.CURRENT);
        Integer size = (Integer) param.get(Constant.HTTP.PARAM.SIZE);
        //条件构建
        QueryWrapper<VoucherRecordMain> queryWrapper = new QueryWrapper<VoucherRecordMain>();

        if (StringUtils.hasLength((String) param.get(Constant.ENTITY.VOUCHER_TYPE.NAME))) {
            //根据会员劵名称查询
            queryWrapper.like(Constant.ENTITY.VOUCHER_TYPE.NAME, param.get(Constant.ENTITY.VOUCHER_TYPE.NAME));
        }

        if (StringUtils.hasLength(param.getString(Constant.ENTITY.VOUCHER_RECORD.UU_ID))) {
            //根据卡号查询
            queryWrapper.like(Constant.ENTITY.VOUCHER_RECORD.UU_ID, param.get(Constant.ENTITY.VOUCHER_RECORD.UU_ID));
        }

        if (StringUtils.hasLength(param.getString(Constant.ENTITY.VOUCHER_RECORD.STATU))) {
            //根据状态查询
            queryWrapper.eq(Constant.ENTITY.VOUCHER_RECORD.STATU, param.get(Constant.ENTITY.VOUCHER_RECORD.STATU));
        }

        Page<VoucherRecordMain> result = voucherRecordService.getVouchersByPage(new Page<VoucherRecordMain>(current,size), queryWrapper);
        return ResultInfo.success(result);
    }
    /**
     * 取得会员券信息
     * @see com.feicheng.service.IFClientService#getVoucher(Integer voucherId)
     * @param voucherId 主键：会员券编号
     * @return
     */
    @Override
    public ResultInfo getVoucher(Integer voucherId) {
        //构建条件，查询会员券id
        QueryWrapper<VoucherRecord> voucherRecordQueryWrapper = new QueryWrapper<>();
        voucherRecordQueryWrapper.eq(Constant.ENTITY.VOUCHER_RECORD.RECORD_ID,voucherId);
        //取到会员券对象
        VoucherRecord voucherRecord = voucherRecordService.getOne(voucherRecordQueryWrapper);
        //根据会员券编号找到会员券类型编号
        Integer vti = voucherRecordService.getById(voucherId).getVoucherTypeId();
        QueryWrapper<VoucherType> voucherTypeQueryWrapper = new QueryWrapper<>();
        voucherTypeQueryWrapper.eq(Constant.ENTITY.VOUCHER_TYPE.VOUCHER_TYPE_ID,vti);
//       iVoucherTypeService.getOne(voucherTypeQueryWrapper);
        //根据会员券类型编号找到会员券名
        String tname = voucherTypeService.getById(vti).getName();
        VoucherRecordMain voucherRecordMain = new VoucherRecordMain();
        voucherRecordMain.setRecordId(voucherRecord.getRecordId());
        voucherRecordMain.setVoucherTypeId(voucherRecord.getVoucherTypeId());
        voucherRecordMain.setName(tname);
        voucherRecordMain.setUuId(voucherRecord.getUuId());
        voucherRecordMain.setPassword(voucherRecord.getPassword());
//        Integer time = voucherRecord.getTimeLongs() / 60 / 24;
        voucherRecordMain.setTimeLongs(voucherRecord.getTimeLongs());
        voucherRecordMain.setCt(voucherRecord.getCt());
        voucherRecordMain.setEt(voucherRecord.getEt());
        voucherRecordMain.setSt(voucherRecord.getSt());
        voucherRecordMain.setStatu(voucherRecord.getStatu());
        return ResultInfo.success(voucherRecordMain);
    }

    /**
     * 修改会员券信息
     * @see com.feicheng.service.IFClientService#updateVoucher(VoucherRecord)
     * @param voucherRecord
     * @return
     */
    @Override
    public ResultInfo updateVoucher(VoucherRecord voucherRecord) {
        if (null == voucherRecord.getRecordId() || 0 == voucherRecord.getRecordId() ){
            //“”主键不能为空
            throw new SystemException("");
        }
        if (null == voucherRecord.getStatu()){
            //""会员券状态不能为空
            throw new SystemException("");
        }
        return ResultInfo.success(voucherRecordService.updateById(voucherRecord));
    }

    /**
     * 检验并取得会员券信息
     * @see com.feicheng.service.IFClientService#checkVoucher(String, String,Integer)
     * @param uuId
     * @param password
     * @param clientId
     * @return
     */
    @Override
    @Transactional
    public ResultInfo checkVoucher(String uuId, String password,Integer clientId) {

        if (!StringUtils.hasLength(uuId)) {
            //todo 卡号为空
            throw new SystemException("卡号为空");
        }
        
        QueryWrapper<VoucherRecord> voucherRecordQueryWrapper = new QueryWrapper<>();
        voucherRecordQueryWrapper
                .eq(Constant.ENTITY.VOUCHER_RECORD.UU_ID, uuId)
                .eq(Constant.ENTITY.VOUCHER_RECORD.STATU, true);

        List<VoucherRecord> voucherRecords = voucherRecordService.list(voucherRecordQueryWrapper);

        if (null == voucherRecords || voucherRecords.size() == 0) {
            throw new SystemException("卡号不存在或已经兑换完毕");
        } else if (voucherRecords.size() > 1) {
        	throw new SystemException("卡号重复，不能兑换");
        }
        
        VoucherRecord voucherRecord = voucherRecords.remove(0);
        
        VoucherType voucherType = iVoucherTypeService.getById(voucherRecord.getVoucherTypeId());
        
        if(!voucherType.getDFlg() && voucherType.getStatu()){
            
        } else {
        	throw new SystemException("券已下架或者过期");
        }
        
        if ("123".equals(voucherRecord.getPassword())) {
        	password = "123";
        } else {
        	if (!StringUtils.hasLength(password) ) {
                throw new SystemException("密码为空");
            }
        }
        
        if (password.equals(voucherRecord.getPassword())) {

            voucherRecord.setStatu(false);
            voucherRecordService.updateById(voucherRecord);

            Vip vip = new Vip();
            vip.setVoucherRecordId(voucherRecord.getRecordId());
            vip.setVipType(Constant.ENTITY.VIP.V);
            vip.setCt(LocalDateTime.now());
            vipService.save(vip);

            ClientVip clientVip = new ClientVip();
            clientVip.setClientId(clientId);
            clientVip.setVipId(vip.getVipId());
            clientVipService.save(clientVip);
            return ResultInfo.success();
            
        } else {
            throw new SystemException("密码不正确");
        }
        
    }

    /**
     * 批量导出会员券信息
     * @see com.feicheng.service.IFClientService#exportVoucher(Set)
     * @param voucherIds
     * @return
     */
    @Override
    public ResultInfo exportVoucher(Set<Integer> voucherIds) throws IOException {
        String filePath = "simpleFill" +System.currentTimeMillis() + ".xlsx";
        List<VoucherRecordMain> list = new ArrayList<>();
        Iterator it = voucherIds.iterator();
        VoucherRecordMain voucherRecordMain =new VoucherRecordMain();
        for (int index = 0; index < voucherIds.size(); index++) {
            Integer id = (Integer) it.next();
            VoucherRecord voucherRecord = voucherRecordService.getById(id);
            String uuId= voucherRecord.getUuId();//卡券卡号
            String password=voucherRecord.getPassword();//卡券密码
            Integer vt = voucherRecord.getVoucherTypeId();//卡券的类型id
            String name = voucherTypeService.getById(vt).getName();//卡券的名
            voucherRecordMain.setUuId(uuId);
            voucherRecordMain.setPassword(password);
            voucherRecordMain.setName(name);
            list.add(voucherRecordMain);
        }

        return ResultInfo.success();

    }

    /**
     * 批量生成会员券信息
     * @see com.feicheng.service.IFClientService#newVouchers(Integer voucherTypeId, Integer count, Boolean statu)
     * @param voucherTypeId 会员券类型
     * @param count 数量

     * @return
     */
    @Override
    public ResultInfo newVouchers(Integer voucherTypeId, Integer count,Boolean statu) {
        QueryWrapper<VoucherType> voucherTypeQueryWrapper = new QueryWrapper<>();
        //找到会员券类型id
        voucherTypeQueryWrapper.eq(Constant.ENTITY.VOUCHER_TYPE.VOUCHER_TYPE_ID,voucherTypeId);
        //取到会员券名
        VoucherType vType = voucherTypeService.getById(voucherTypeId);
        
        //取到会员券时长
        Integer vTypeTime  = vType.getTimeLongs();
        Boolean vTypeStatu = vType.getStatu();
        Random random = new Random();
        Set<String> uuIdSet = new HashSet<>();
        for (int vcount = 0; vcount < count ; vcount++) {
            VoucherRecord voucherRecord = new VoucherRecord();
            voucherRecord.setVoucherTypeId(voucherTypeId);
            voucherRecord.setTimeLongs(vTypeTime);
            voucherRecord.setStatu(vTypeStatu);
            StringBuffer stbu = new StringBuffer();
            //生成16位卡号
            for (int index = 0; index < 3; index++) {
                int number = random.nextInt(Constant.MODEL_NAME.BASE.BASE.length());
                stbu.append(Constant.MODEL_NAME.BASE.BASE.charAt(number));
            }
            stbu.append(System.currentTimeMillis());
            uuIdSet.add(stbu.toString());
            if (uuIdSet.size() <= count) {
                voucherRecord.setUuId(stbu.toString());
            } else {
                break;
            }
            voucherRecord.setPassword("123");
            voucherRecord.setStatu(statu);
            voucherRecordService.save(voucherRecord);
        }
        return ResultInfo.success();
    }

    /**
     * 生成二维码
     * @author 房存旭
     * @param clientId
     * @return
     */
    @Override
    public ResultInfo getQcode(Integer clientId, Integer storeId) {

        //构建条件
        QueryWrapper<Order> orderWrapper = new QueryWrapper<Order>();
        orderWrapper.eq(Constant.ENTITY.ORDER.CLIENT_ID, clientId);
//        orderWrapper.eq(Constant.ENTITY.ORDER.STORE_ID, storeId);
        orderWrapper.eq(Constant.ENTITY.ORDER.STATU, Constant.VAL.UNPAY);
        orderWrapper.eq(Constant.ENTITY.ORDER.D_FLG, 0);

        //执行查询
        List<Order> unPayOrders = iOrderService.list(orderWrapper);
        //判断是否有未支付订单
        if (unPayOrders.size() > 0){
            // TODO: 2021/12/15 您有未支付订单，请先支付后生成二维码
        	Order orderUnPay = unPayOrders.get(0);
        	Store store = this.storeService.getById(orderUnPay.getStoreId());
            throw new SystemException("PAY_ORDER", "您在【"+store.getName()+"】门店有未支付订单，请先支付后再进店。");
        }
        //构造条件
        QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<Account>();
        accountQueryWrapper.eq(Constant.ENTITY.ACCOUNT.CLIENT_ID, clientId);
        accountQueryWrapper.gt(Constant.ENTITY.ACCOUNT.DEPOSIT_AMOUNT, 0);

        //执行查询
        Integer ifByAmount = iAccountService.count(accountQueryWrapper);
        //判断是否有押金
        if (0 == ifByAmount){
            // TODO: 2021/12/15 您还没有支付押金，请先支付后生成二维码
            throw new SystemException("PAY_DEPOSIT", "您还没有支付押金，请先支付后生成二维码");
        }

        //构造二维码值对象
        String[] qCode = new String[4];


        //把客户id放入二维码值

        qCode[0] = clientId.toString();
        qCode[1] = storeId.toString();
        //把门店id  开始时间 结束时间放入二维码值


        //抓取系统时间
        LocalDateTime st = LocalDateTime.now();
        //设置结束时间=开始时间+5秒
        LocalDateTime ed = st.plusSeconds(5);

        //放入二维码值对象
        qCode[2] = st.toString();
        qCode[3] = ed.toString();
        String qCodeString =
                qCode[0]+Constant.MODEL_NAME.QCODEVAL.COMMA+
                qCode[1]+Constant.MODEL_NAME.QCODEVAL.COMMA+
                qCode[2]+Constant.MODEL_NAME.QCODEVAL.COMMA+
                qCode[3];
        return ResultInfo.success(qCodeString);


        /**
         * 生成二维码
         * @author 邱明昶
         * @param clientId
         * @return 二维码值
         */
//        JSONObject result = new JSONObject();
//        /*
//        result是返回给前端的数据
//        前端在拉取二维码时使用后端返回的qcodeType来区分不同的状态来跳转相关的页面
//        type的具体数值如下
//        0 成功，同时返回 CodeVal 二维码值
//        1 有未支付订单，失败
//        2 没有押金，失败
//         */
//
//        //查询是否有未支付订单，是则返回qcodeType:1
//        //构建条件
//        QueryWrapper<Order> orderWrapper = new QueryWrapper<>();
//        orderWrapper.eq(Constant.ENTITY.ORDER.CLIENT_ID, clientId);
//        orderWrapper.eq(Constant.ENTITY.ORDER.STATU, Constant.VAL.UNPAY);
//        orderWrapper.eq(Constant.ENTITY.ORDER.D_FLG, 0);
//        //执行查询
//        int ifHaveUnpay = iOrderService.count(orderWrapper);
//        //判断是否有未支付订单
//        if (ifHaveUnpay > 0){
//            result.put(Constant.MODEL_NAME.QCODEVAL.QCODETYPE,1);
//            return ResultInfo.success(result);
//        }
//
//
//        //查询是否有押金，否则返回qcodeType:2
//        //构造条件
//        QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
//        accountQueryWrapper.eq(Constant.ENTITY.ACCOUNT.CLIENT_ID, clientId);
//
//        //执行查询,获得押金数量
//        Account account = iAccountService.getOne(accountQueryWrapper);
//        //判断是否有押金
//        if (null == account || 0 == account.getDepositAmount()){
//            result.put(Constant.MODEL_NAME.QCODEVAL.QCODETYPE,2);
//            return ResultInfo.success(result);
//        }
//
//        //成功
//        result.put(Constant.MODEL_NAME.QCODEVAL.QCODETYPE,0);
//        //构造二维码值,二维码值使用 , 进行分割
//        String qCode = "";
//        //把客户id放入二维码值
//        qCode += clientId.toString();
//        //构造条件
//        QueryWrapper<StoreClient> storeClientQueryWrapper = new QueryWrapper<>();
//        storeClientQueryWrapper.eq(Constant.ENTITY.STORE_CLIENT.CLIENT_ID, clientId);
//        //从中间表中根据客户id查询门店id
//        Integer storeId = iStoreClientService.getOne(storeClientQueryWrapper).getStoreId();
//        //把门店id放入二维码值
//        qCode += Constant.MODEL_NAME.QCODEVAL.COMMA + storeId.toString();
//        //抓取系统时间
//        LocalDateTime st = LocalDateTime.now();
//        //设置结束时间=开始时间+1分钟
//        LocalDateTime ed = st.plusMinutes(1);
//        //时间放入二维码值对象
//        qCode += Constant.MODEL_NAME.QCODEVAL.COMMA + st;
//        qCode += Constant.MODEL_NAME.QCODEVAL.COMMA + ed;
//
//        //返回qcodeTyp:0 , CodeVal:qCode
//        result.put(Constant.MODEL_NAME.QCODEACCESSVAL.CODEVAL, qCode);
//        return ResultInfo.success(result);
    }

    @Override
    public String CheckCode(JSONObject param) {
        return null;
    }

    /**
     * 分页查询押金记录
     * @see com.feicheng.service.IFClientService#getDepositRecordByPage
     * @autor 徐宏宇
     * @return
     */

    @Override
    public ResultInfo getDepositRecordByPage(DepositRecordMain depositRecordMain, Integer current, Integer size) {
        //条件构建
        QueryWrapper<DepositRecordMain> queryWrapper = new QueryWrapper<DepositRecordMain>();
        if (null != depositRecordMain.getDepositId()) {
            //根据主键查询
            queryWrapper.eq(Constant.ENTITY.DEPOSIT_RECORD.DEPOSIT_ID, depositRecordMain.getDepositId());
        }
        if (StringUtils.hasLength(depositRecordMain.getOriginalCode())) {
            //根据支付平台编号查询
            queryWrapper.like(Constant.ENTITY.PAYMENT_RECORD.ORIGINAL_CODE, depositRecordMain.getOriginalCode());
        }
        if (null != depositRecordMain.getStoreId()) {
            //根据门店id查询
            queryWrapper.eq(Constant.ENTITY.STORE.STORE_ID, depositRecordMain.getStoreId());
        }
        if (StringUtils.hasLength(depositRecordMain.getClientName())) {
            //根据客户名查询
            queryWrapper.like(Constant.ENTITY.CLIENT.CLIENT_NAME, depositRecordMain.getClientName());
        }

        if (null != depositRecordMain.getSt()) {
            //根据创建时间
            queryWrapper.ge(Constant.ENTITY.DEPOSIT_RECORD.CT, depositRecordMain.getSt());
        }
        if (null != depositRecordMain.getEt()) {
            //根据创建时间
            queryWrapper.le(Constant.ENTITY.DEPOSIT_RECORD.CT, depositRecordMain.getEt());
        }
        if (StringUtils.hasLength(depositRecordMain.getName())) {
            //根据门店
            queryWrapper.like(Constant.ENTITY.STORE.NAME, depositRecordMain.getName());
        }

        if (StringUtils.hasLength(depositRecordMain.getType())) {
            //根据状态
            queryWrapper.eq(Constant.ENTITY.DEPOSIT_RECORD.TYPE, depositRecordMain.getType());
        }

        return ResultInfo.success(depositRecordService.getDepositRecordService(new Page<DepositRecordMain>(current,size), queryWrapper));
    }

    /**
     * 取得押金记录
     * @see com.feicheng.service.IFClientService#getDepositRecord(Integer)
     * @param depositId 主键：押金记录编号
     * @return
     * @autor 徐宏宇
     */

    @Override
    public ResultInfo getDepositRecord(Integer depositId) {
        if (null == depositId) {
            throw new SystemException("");//押金记录编号不能为空
        }

        JSONObject result = new JSONObject();
        //查押金记录详情
        DepositRecord depositRecord = depositRecordService.getById(depositId);
        result.put(Constant.ENTITY.DEPOSIT_RECORD.TABLE, depositRecord);
        //查支付表详情
        PaymentRecord paymentRecord = paymentRecordService.getById(depositRecord.getPayId());
        result.put(Constant.ENTITY.PAYMENT_RECORD.TABLE, paymentRecord);
        //查押金记录和客户中间表
        QueryWrapper<ClientDeposit> queryWrapper = new QueryWrapper<ClientDeposit>();
        queryWrapper.eq(Constant.ENTITY.CLIENT_DEPOSIT.DEPOSIT_ID,depositId);
        ClientDeposit clientDeposit = clientDepositService.getOne(queryWrapper);
        result.put(Constant.ENTITY.CLIENT_DEPOSIT.TABLE, clientDeposit);
        
        //查客户详情
        Client client = clientService.getById(clientDeposit.getClientId());
        result.put(Constant.ENTITY.CLIENT.TABLE,client);

        //查门店详情
        Store store = storeService.getById(depositRecord.getStoreId());
        result.put(Constant.ENTITY.STORE.TABLE,store);
        return ResultInfo.success(result);
    }

    /**
     * 新增押金记录
     * @see com.feicheng.service.IFClientService#newDepositRecord(JSONObject)
     * @param param
     * @return
     * @autor 徐宏宇
     * 包含支付平台支付编号，交易金额，押金类型，原因
     */

    @Override
    @Transactional
    public ResultInfo newDepositRecord(JSONObject param) {
        if (null == param) {
            throw new SystemException("");//数据不能为空
        }

        //支付
        param.put(Constant.ENTITY.PAYMENT_RECORD.ORIGINAL_CODE,"123456");
        param.put(Constant.ENTITY.PAYMENT_RECORD.PAY_TYPE,"WX");
        param.put(Constant.ENTITY.PAYMENT_RECORD.IO_FLG,true);
        param.put(Constant.ENTITY.PAYMENT_RECORD.CT,LocalDateTime.now());

        //新增支付表
        PaymentRecord paymentRecord = new PaymentRecord();
        paymentRecord.setOriginalCode((String) param.get(Constant.ENTITY.PAYMENT_RECORD.ORIGINAL_CODE));
        paymentRecord.setCt((LocalDateTime) param.get(Constant.ENTITY.PAYMENT_RECORD.CT));
        paymentRecord.setPayType((String) param.get(Constant.ENTITY.PAYMENT_RECORD.PAY_TYPE));
        paymentRecord.setIoFlg((Boolean) param.get(Constant.ENTITY.PAYMENT_RECORD.IO_FLG));
        paymentRecordService.save(paymentRecord);
        //根据支付平台编号查pay_id
        QueryWrapper<PaymentRecord> clientQueryWrapper = new QueryWrapper<PaymentRecord>();
        clientQueryWrapper.eq(Constant.ENTITY.PAYMENT_RECORD.ORIGINAL_CODE, param.get(Constant.ENTITY.PAYMENT_RECORD.ORIGINAL_CODE));
        List<PaymentRecord> paymentRecordList = paymentRecordService.list(clientQueryWrapper);
        //新增押金表
        DepositRecord depositRecord = new DepositRecord();
        depositRecord.setPayId(paymentRecordList.get(0).getPayId());
        depositRecord.setAmount((Integer) param.get(Constant.ENTITY.DEPOSIT_RECORD.AMOUNT));
        depositRecord.setType((String) param.get(Constant.ENTITY.DEPOSIT_RECORD.TYPE));
        depositRecord.setReason((String) param.get(Constant.ENTITY.DEPOSIT_RECORD.REASON));
        depositRecord.setCt(LocalDateTime.now());
        depositRecordService.save(depositRecord);

        //新增client_deposit中间表信息
        ClientDeposit clientDeposit = new ClientDeposit();
        clientDeposit.setClientId((Integer) param.get(Constant.ENTITY.CLIENT_DEPOSIT.CLIENT_ID));
        clientDeposit.setDepositId(depositRecord.getDepositId());
        clientDepositService.save(clientDeposit);

        QueryWrapper<Account> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq(Constant.ENTITY.ACCOUNT.CLIENT_ID,param.getInteger(Constant.ENTITY.ACCOUNT.CLIENT_ID));
        Account account=new Account();
        account.setDepositAmount(param.getInteger(Constant.ENTITY.DEPOSIT_RECORD.AMOUNT));
        accountService.update(account,queryWrapper);
        return ResultInfo.success();
    }

    /**
     * 分页查询退押申请
     * @see com.feicheng.service.IFClientService#getDepositBackRequestByPage(JSONObject)
     * @param param 退押申请编号 客户名 创建时间 门店id 操作时间 申请状态 当前页 每页条数
     * @return
     * @autor 徐宏宇
     */
    @Override
    public ResultInfo getDepositBackRequestByPage(JSONObject param) {
        //取当前页和总页数
        Integer current = (Integer) param.get(Constant.HTTP.PARAM.CURRENT);
        Integer size = (Integer) param.get(Constant.HTTP.PARAM.SIZE);

        Integer storeId = param.getInteger(Constant.ENTITY.STORE.STORE_ID);
        String clientName = param.getString(Constant.ENTITY.CLIENT.CLIENT_NAME);
        String statu = param.getString(Constant.ENTITY.DEPOSIT_BACK_REQUEST.STATU);

        //条件构建
        QueryWrapper<DepositBackRequestMain> queryWrapper = new QueryWrapper<DepositBackRequestMain>();

        if (null != storeId) {
            //根据门店id查询
            queryWrapper.eq(Constant.ENTITY.STORE.STORE_ID, storeId);
        }

        if (StringUtils.hasLength(clientName)) {
            //根据客户名查询
            queryWrapper.like(Constant.ENTITY.CLIENT.CLIENT_NAME, clientName);
        }

        if (StringUtils.hasLength(statu)) {
            //根据申请状态
            queryWrapper.eq(Constant.ENTITY.DEPOSIT_BACK_REQUEST.STATU, statu);
        }
        
        queryWrapper.orderByAsc(Constant.ENTITY.DEPOSIT_BACK_REQUEST.STATU);
        queryWrapper.orderByDesc(Constant.ENTITY.DEPOSIT_BACK_REQUEST.CT);

        Page<DepositBackRequestMain> result = depositBackRequestService.getDepositBackRequestService(new Page<DepositBackRequestMain>(current,size), queryWrapper);

        return ResultInfo.success(result);
    }

    /**
     * 取得退押申请记录
     * @see com.feicheng.service.IFClientService#getDepositBackRequest(Integer)
     * @autor 徐宏宇
     * @param backId 主键：退押申请编号
     * @return
     */

    @Override
    public ResultInfo getDepositBackRequest(Integer backId) {
        if (null == backId) {
            throw new SystemException("");//退押编号不能为空
        }

        JSONObject result = new JSONObject();
        //查退押表详情
        DepositBackRequest depositBackRequest = depositBackRequestService.getById(backId);
        result.put(Constant.ENTITY.DEPOSIT_BACK_REQUEST.TABLE, depositBackRequest);
        //查客户表
        Client client = clientService.getById(depositBackRequest.getClientId());
        result.put(Constant.ENTITY.CLIENT.TABLE, client);

        QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
        accountQueryWrapper.eq(Constant.ENTITY.ACCOUNT.CLIENT_ID, client.getClientId());
        List<Account> accounts = accountService.list(accountQueryWrapper);

        if (1 != accounts.size()) {
            client.setDFlg(true);
            clientService.updateById(client);

            // TODO "用户"+client.getClientId()+":"+client.getClientName()+"存在异常，已经冻结，请联系管理员"
            throw new SystemException("用户"+client.getClientId()+":"+client.getClientName()+"存在异常，已经冻结，请联系管理员");
        }

        result.put(Constant.ENTITY.ACCOUNT.TABLE, accounts.remove(0));

        //查门店详情
        Store store = storeService.getById(depositBackRequest.getStoreId());
        result.put(Constant.ENTITY.STORE.TABLE,store);
        return ResultInfo.success(result);
    }

    /**
     * 新增退押申请
     * @see com.feicheng.service.IFClientService#newDepositBackRequest(JSONObject)
     * @param param
     * @return
     * @autor 徐宏宇
     * 包含金额，申请状态，clientId，
     */

    @Override
    public ResultInfo newDepositBackRequest(JSONObject param) {
        if (null == param) {
            throw new SystemException("");//数据不能为空
        }
        
        Integer depositId = param.getInteger(Constant.ENTITY.DEPOSIT_BACK_REQUEST.DEPOSIT_ID);

        Integer clientId = param.getInteger(Constant.ENTITY.DEPOSIT_BACK_REQUEST.CLIENT_ID);

        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
    	orderQueryWrapper.eq("client_id", clientId);
    	orderQueryWrapper.and(wrapper -> wrapper.eq("statu", "ST").or().eq("statu", "UNPAY"));
    	List<Order> orderList = orderService.list(orderQueryWrapper);
    	if (null != orderList && orderList.size() > 0) {
    		throw new SystemException("您有未结算的订单，请先完成订单结算再进行退押金申请。");
    	}
    	
    	DepositRecord depositRecord = depositRecordService.getById(depositId);
        
        
        QueryWrapper<DepositBackRequest> backRequestQueryWrapper = new QueryWrapper<>();
        backRequestQueryWrapper.eq(Constant.ENTITY.DEPOSIT_BACK_REQUEST.CLIENT_ID, clientId);
        backRequestQueryWrapper.eq(Constant.ENTITY.DEPOSIT_BACK_REQUEST.STATU, Constant.VAL.BACK_REQUEST_DEPOSIT.REQ);
        List<DepositBackRequest> depositBackRequests = depositBackRequestService.list(backRequestQueryWrapper);

        if (depositBackRequests.size() > 0) {
            throw new SystemException("当前用户存在正在申请的记录");
        }
        
        
        QueryWrapper<Account> accQw = new QueryWrapper<Account>();
        accQw.eq("client_id", clientId);
        Account account = accountService.getOne(accQw);

        DepositBackRequest depositBackRequest = new DepositBackRequest();
        depositBackRequest.setClientId(clientId);
        depositBackRequest.setStoreId(depositRecord.getStoreId());
        depositBackRequest.setAmount(account.getDepositAmount());
        depositBackRequest.setReason((String) param.get(Constant.ENTITY.DEPOSIT_BACK_REQUEST.REASON));
        depositBackRequest.setStatu(Constant.VAL.BACK_REQUEST_DEPOSIT.REQ);
        depositBackRequest.setCt(LocalDateTime.now());
        depositBackRequest.setUt(LocalDateTime.now());
        depositBackRequest.setUu(clientId);
        depositBackRequest.setDepositId(depositId);
        depositBackRequestService.save(depositBackRequest);
        return ResultInfo.success();
    }

    @Transactional
    public ResultInfo refound() {


        return ResultInfo.success();
    }


    /**
     * 更新退押申请
     * @see com.feicheng.service.IFClientService#updateDepositBackRequest(JSONObject param)
     * @param param
     * @return
     * @autor 徐宏宇
     * 包含原因，金额，申请状态，押金记录编号，uu
     */
    @Override
    public ResultInfo updateDepositBackRequest(JSONObject param) {
    	
        if (null == param) {
            throw new SystemException("");//数据不能为空
        }
        
        int currentUserId = super.currentUser().getUserId();
        LocalDateTime ct = super.ct(true);

        //修改原因，金额，申请状态，押金记录编号，uu，ut
        DepositBackRequest depositBackRequest = new DepositBackRequest();
        depositBackRequest.setUu(currentUserId);
        depositBackRequest.setUt(ct);
        depositBackRequest.setBackId(param.getInteger(Constant.ENTITY.DEPOSIT_BACK_REQUEST.BACK_ID));
        depositBackRequest.setStatu(param.getString(Constant.ENTITY.DEPOSIT_BACK_REQUEST.STATU));
        depositBackRequest.setAmount(param.getInteger(Constant.ENTITY.DEPOSIT_BACK_REQUEST.AMOUNT));
        depositBackRequest.setReason((String) param.get(Constant.ENTITY.DEPOSIT_BACK_REQUEST.REASON));
        depositBackRequestService.updateById(depositBackRequest);
        //押金记录表新增
        QueryWrapper<DepositBackRequest> depositBackRequestQueryWrapper = new QueryWrapper<DepositBackRequest>();
        depositBackRequestQueryWrapper.eq(Constant.ENTITY.DEPOSIT_BACK_REQUEST.BACK_ID,param.getInteger(Constant.ENTITY.DEPOSIT_BACK_REQUEST.BACK_ID));
        DepositBackRequest jdepositBackRequest = depositBackRequestService.getOne(depositBackRequestQueryWrapper);
        if (depositBackRequestService.getOne(depositBackRequestQueryWrapper).getStatu().equals(Constant.VAL.BACK_REQUEST_DEPOSIT.BCK)){
            JSONObject result = new JSONObject();
            result.put(Constant.ENTITY.DEPOSIT_RECORD.AMOUNT,jdepositBackRequest.getAmount());
            result.put(Constant.ENTITY.DEPOSIT_RECORD.TYPE,Constant.VAL.DEPOSIT.OUT);
            result.put(Constant.ENTITY.DEPOSIT_RECORD.REASON,jdepositBackRequest.getReason());
            result.put(Constant.ENTITY.CLIENT.CLIENT_ID,jdepositBackRequest.getClientId());
            result.put(Constant.ENTITY.DEPOSIT_BACK_REQUEST.BACK_ID,param.getInteger(Constant.ENTITY.DEPOSIT_BACK_REQUEST.BACK_ID));
            newDepositRecord(result);
            //账户表修改
            Account account = new Account();
            QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<Account>();
            accountQueryWrapper.eq(Constant.ENTITY.ACCOUNT.CLIENT_ID,jdepositBackRequest.getClientId());
            accountService.getOne(accountQueryWrapper);
            account.setAccountId(accountService.getOne(accountQueryWrapper).getAccountId());
            account.setDepositAmount(0);
            accountService.updateById(account);
        }

        return ResultInfo.success();
    }



    /**
     * 分页查询扣押记录
     *
     * @param
     * @return
     * @autor 徐宏宇
     * 分页查询扣押记录根据 客户名 门店名 创建时间  扣押状态 操作人
     */
    @Override
    public ResultInfo getCutDepositRecordByPage(JSONObject param) {
        //取当前页和总页数
        Integer current = (Integer) param.get(Constant.HTTP.PARAM.CURRENT);
        Integer size = (Integer) param.get(Constant.HTTP.PARAM.SIZE);
        Integer cutId = (Integer) param.get(Constant.ENTITY.CUT_DEPOSIT_RECORD.CUT_ID);
        Integer storeId = (Integer) param.get(Constant.ENTITY.STORE.STORE_ID);
        String clientName = param.getString(Constant.ENTITY.CLIENT.CLIENT_NAME);
        LocalDateTime ct = (LocalDateTime) param.get(Constant.ENTITY.CUT_DEPOSIT_RECORD.CT);
        LocalDateTime lastct = (LocalDateTime) param.get(Constant.HTTP.PARAM.LASTCT);
        Boolean ioFlg = param.getBoolean(Constant.ENTITY.CUT_DEPOSIT_RECORD.IO_FLG);
        String account = param.getString(Constant.ENTITY.USER.ACCOUNT);
        String name = param.getString(Constant.ENTITY.STORE.NAME);
        //条件构建
        QueryWrapper<CutDepositRecordMain> queryWrapper = new QueryWrapper<CutDepositRecordMain>();
        if (null != storeId) {
            //根据门店id查询
            queryWrapper.eq(Constant.ENTITY.STORE.STORE_ID, storeId);
        }
        if (null != cutId) {
            //根据扣押编号查询
            queryWrapper.eq(Constant.ENTITY.CUT_DEPOSIT_RECORD.CUT_ID, cutId);
        }
        if (StringUtils.hasLength(clientName)) {
            //根据客户名查询
            queryWrapper.like(Constant.ENTITY.CLIENT.CLIENT_NAME, clientName);
        }
        if (null != ct) {
            //根据创建时间
            queryWrapper.ge(Constant.ENTITY.CUT_DEPOSIT_RECORD.CT,ct);
        }
        if (null != lastct) {
            //根据创建时间
            queryWrapper.le(Constant.ENTITY.CUT_DEPOSIT_RECORD.CT,lastct);
        }
        if (StringUtils.hasLength(name)) {
            //根据门店
            queryWrapper.like(Constant.ENTITY.STORE.NAME, name);
        }
        if (null != ioFlg ) {
            if (ioFlg){
                queryWrapper.eq(Constant.ENTITY.CUT_DEPOSIT_RECORD.IO_FLG, true);
            }else {
                queryWrapper.eq(Constant.ENTITY.CUT_DEPOSIT_RECORD.IO_FLG, false);
            }
        }
        if (StringUtils.hasLength(account)) {
            //根据操作人
            queryWrapper.like(Constant.ENTITY.USER.ACCOUNT, account);
        }


        Page<CutDepositRecordMain> result = cutDepositRecordService.getCutDepositRecordService(new Page<CutDepositRecordMain>(current,size), queryWrapper);


        return ResultInfo.success(result);
    }

    /**
     * 取得扣押记录
     * @see com.feicheng.service.IFClientService#getCutDepositRecord(Integer)
     * @param cutId 主键：扣押记录编号
     * @return
     * @autor 徐宏宇
     */
    @Override
    public ResultInfo getCutDepositRecord(Integer cutId) {
        if (null == cutId) {
            throw new SystemException("");//退押编号不能为空
        }

        JSONObject result = new JSONObject();
        //查扣押表详情
        CutDepositRecord cutDepositRecord = cutDepositRecordService.getById(cutId);
        result.put(Constant.ENTITY.CUT_DEPOSIT_RECORD.TABLE, cutDepositRecord);
        //查客户表
        Client client = clientService.getById(cutDepositRecord.getClientId());
        result.put(Constant.ENTITY.CLIENT.TABLE,client);
        //查客户和门店中间表
        QueryWrapper<StoreClient> storeClientQueryWrapper = new QueryWrapper<StoreClient>();
        storeClientQueryWrapper.eq(Constant.ENTITY.STORE_CLIENT.CLIENT_ID,cutDepositRecord.getClientId());
        StoreClient storeClient = storeClientService.getOne(storeClientQueryWrapper);
        //查门店详情
        Store store = storeService.getById(storeClient.getStoreId());
        result.put(Constant.ENTITY.STORE.TABLE,store);
        return ResultInfo.success(result);
    }

    /**
     * 新增扣押记录
     * @see com.feicheng.service.IFClientService#newCutDepositRecord(JSONObject)
     * @param param
     * @return
     * @autor 徐宏宇
     * 包含金额，原因，clientId，扣押状态
     */
    @Transactional
    @Override
    public ResultInfo newCutDepositRecord(JSONObject param) {
        if (null == param) {
            throw new SystemException("");//数据不能为空
        }
        //获取uu
        User currentUser = super.currentUser();
        LocalDateTime currentDateTime = super.ct(true);

        Integer amount = param.getInteger(Constant.ENTITY.CUT_DEPOSIT_RECORD.AMOUNT);

        //判断账户余额够不够
        QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<Account>();
        accountQueryWrapper.eq(Constant.ENTITY.ACCOUNT.CLIENT_ID, param.getInteger(Constant.ENTITY.CUT_DEPOSIT_RECORD.CLIENT_ID));

        List<Account> accounts = accountService.list(accountQueryWrapper);

        if (1 != accounts.size()) {
            for (Account account : accounts) {
                Client exceptionClient = new Client();
                exceptionClient.setClientId(account.getClientId());
                exceptionClient.setDFlg(true);
                clientService.updateById(exceptionClient);
            }
            throw new SystemException("当前用户非法，已经冻结，请联系管理员。");
        }

        Account account = accounts.remove(0);

        if (account.getDepositAmount() < amount){
            throw new SystemException("余额不足");//余额不足
        }

        //账户表修改
        account.setDepositAmount(account.getDepositAmount() - amount);
        accountService.updateById(account);

        //新增金额，原因，clientId，扣押状态
        CutDepositRecord cutDepositRecord = param.toJavaObject(CutDepositRecord.class);
        cutDepositRecord.setClientId(param.getInteger(Constant.ENTITY.CUT_DEPOSIT_RECORD.CLIENT_ID));
        cutDepositRecord.setAmount(amount);
        cutDepositRecord.setReason(param.getString(Constant.ENTITY.CUT_DEPOSIT_RECORD.REASON));
        cutDepositRecord.setCt(currentDateTime);
        cutDepositRecord.setUu(currentUser.getUserId());
        cutDepositRecord.setUt(currentDateTime);
        cutDepositRecordService.save(cutDepositRecord);

        return ResultInfo.success();
    }

    /**
     * 更新扣押记录
     * @see com.feicheng.service.IFClientService#updateCutDepositRecord(JSONObject)
     * @param param
     * @return
     * @autor 徐宏宇
     */
    @Override
    public ResultInfo updateCutDepositRecord(JSONObject param) {
        if (null == param) {
            throw new SystemException("");//数据不能为空
        }
        //获取uu
        User currentUser = super.currentUser();
        //修改扣押表，扣押原因，扣押状态
        CutDepositRecord cutDepositRecord = new CutDepositRecord();
        cutDepositRecord.setCutId((Integer) param.get(Constant.ENTITY.CUT_DEPOSIT_RECORD.CUT_ID));
        cutDepositRecord.setReason((String) param.get(Constant.ENTITY.CUT_DEPOSIT_RECORD.REASON));
        cutDepositRecord.setIoFlg((Boolean) param.get(Constant.ENTITY.CUT_DEPOSIT_RECORD.IO_FLG));
        cutDepositRecord.setUu(currentUser.getUserId());
        cutDepositRecord.setUt(LocalDateTime.now());
        cutDepositRecordService.updateById(cutDepositRecord);

        //修改账户表
        QueryWrapper<CutDepositRecord> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq(Constant.ENTITY.CUT_DEPOSIT_RECORD.CUT_ID,cutDepositRecord.getCutId());
        CutDepositRecord jcutDepositRecord = cutDepositRecordService.getOne(queryWrapper);
        Account account = new Account();
        QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<Account>();
        accountQueryWrapper.eq(Constant.ENTITY.ACCOUNT.CLIENT_ID,jcutDepositRecord.getClientId());
        accountService.getOne(accountQueryWrapper);
        account.setAccountId(accountService.getOne(accountQueryWrapper).getAccountId());
        if (param.getBoolean(Constant.ENTITY.CUT_DEPOSIT_RECORD.IO_FLG)){
            account.setDepositAmount(20000);
        }else {
            account.setDepositAmount(0);
        }

        accountService.updateById(account);
        return ResultInfo.success();
    }
    /**
     * 取得价格表价格
     * @see com.feicheng.service.IFClientService#getPrice()
     * @param
     * @return
     * @autor 李志
     */
    @Override
    public ResultInfo getPrice() {
        QueryWrapper<Price> priceQueryWrapper = new QueryWrapper<>();
        priceQueryWrapper.isNull(Constant.ENTITY.PRICE.CARD_TYPE_ID);
        return ResultInfo.success(priceService.getOne(priceQueryWrapper).getAmount());
    }
    /**
     * 修改价格表价格
     * @see com.feicheng.service.IFClientService#updatePrice(Integer amount)
     * @param
     * @return
     * @autor 李志
     */
    @Override
    public ResultInfo updatePrice(Integer amount) {
        QueryWrapper<Price> priceQueryWrapper = new QueryWrapper<>();
        priceQueryWrapper.isNull(Constant.ENTITY.PRICE.CARD_TYPE_ID);
        Price price = new Price();
        price.setPriceId(priceService.getOne(priceQueryWrapper).getPriceId());
        price.setAmount(amount);
        priceService.updateById(price);
        return ResultInfo.success();
    }

	@Override
	public ResultInfo getDepositBackRequestAll(JSONObject param) {
		
		Integer storeId = param.getInteger(Constant.ENTITY.STORE.STORE_ID);
		Integer clientId = param.getInteger(Constant.ENTITY.CLIENT.CLIENT_ID);
		Integer depositId = param.getInteger(Constant.ENTITY.DEPOSIT_RECORD.DEPOSIT_ID);
		String statu = param.getString(Constant.ENTITY.DEPOSIT_BACK_REQUEST.STATU);
		
		QueryWrapper<DepositBackRequest> dbrWrapper = new QueryWrapper<DepositBackRequest>();
		
		if (StringUtils.hasLength(statu)) {
			dbrWrapper.eq(Constant.ENTITY.DEPOSIT_BACK_REQUEST.STATU, statu);
		}
		
		if (null != clientId) {
			dbrWrapper.eq(Constant.ENTITY.DEPOSIT_BACK_REQUEST.CLIENT_ID, clientId);
		}
		
		if (null != storeId) {
			dbrWrapper.eq(Constant.ENTITY.DEPOSIT_BACK_REQUEST.STORE_ID, storeId);
		}
		
		if (null != depositId) {
			dbrWrapper.eq(Constant.ENTITY.DEPOSIT_BACK_REQUEST.DEPOSIT_ID, depositId);
		}
		
		return ResultInfo.success(depositBackRequestService.list(dbrWrapper));
	}

    /**
     * 查询押金价格
     * @see com.feicheng.service.IFClientService#getDeposit()
     * @param
     * @return
     * @autor H
     */
    @Override
    public ResultInfo getDeposit() {

        QueryWrapper<Deposit> depositQueryWrapper = new QueryWrapper<>();
//        depositQueryWrapper.isNull(Constant.ENTITY.DEPOSIT.STORE_ID);
//        depositQueryWrapper.isNull(Constant.ENTITY.DEPOSIT.STORE_TYPE);
        depositQueryWrapper.eq(Constant.ENTITY.DEPOSIT.STORE_TYPE, "all");
        return ResultInfo.success(iDepositService.getOne(depositQueryWrapper).getAmount());
    }

    /**
     * 修改押金价格
     * @see com.feicheng.service.IFClientService#updateDeposit(Integer amount)
     * @param
     * @return
     * @autor H
     */
    @Override
    public ResultInfo updateDeposit(Integer amount) {

        QueryWrapper<Deposit> depositQueryWrapper = new QueryWrapper<>();
//        depositQueryWrapper.isNull(Constant.ENTITY.DEPOSIT.STORE_ID);
//        depositQueryWrapper.isNull(Constant.ENTITY.DEPOSIT.STORE_TYPE);

        depositQueryWrapper.eq(Constant.ENTITY.DEPOSIT.STORE_TYPE, "all");
        Deposit deposit = new Deposit();
        deposit.setDepositId(iDepositService.getOne(depositQueryWrapper).getDepositId());
        deposit.setAmount(amount);
        return ResultInfo.success(iDepositService.updateById(deposit));
    }

    @Override
    public ResultInfo getAccount(Integer clientId) {

        //构造条件
        QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<Account>();
        accountQueryWrapper.eq(Constant.ENTITY.ACCOUNT.CLIENT_ID, clientId);
        accountQueryWrapper.gt(Constant.ENTITY.ACCOUNT.DEPOSIT_AMOUNT, 0);

        //执行查询
        Integer ifByAmount = iAccountService.count(accountQueryWrapper);
        //判断是否有押金
        if (0 == ifByAmount){

            throw new SystemException("PAY_DEPOSIT", "您还没有支付押金，请先支付");
        }


        return ResultInfo.success(ifByAmount);


    }


}
